import 'package:get/get.dart';
import 'package:hygf/http/file_transfer/down_cap.dart';
import 'package:hygf/page/home/approval/un_approval/logic.dart';

import '../../../../bean/CustomerList.dart';
import '../../../../config/ApiConfig.dart';
import '../../../../http/DioClient.dart';
import '../../../../http/core/ResponseData.dart';
import '../../../../util/HDNavigator.dart';
import '../../../../util/LoadingUtils.dart';
import '../../../../view/image_picker_widget.dart';
import '../CustomerDetailPage.dart';
import 'state.dart';

class StartApprovalPageLogic extends GetxController {
  final StartApprovalState state = StartApprovalState();

  Future<void> initImgUrl() async {
    print("initImgUrl------start_approval");
    if((state.startedApproval.designPhoto??"").isNotEmpty){
      List<ImagePickerData> designPhotoData = await DioClient().requestImgUrl(state.startedApproval.designPhoto);
      state.designPhotoController.resetImages([]);
      state.designPhotoFileController.resetFiles([]);
      state.designPhotoController.addImages(designPhotoData);
      state.designPhotoFileController.addFiles(designPhotoData);
    }
    if((state.startedApproval.contract??"").isNotEmpty ){
      List<ImagePickerData> contractData = await DioClient().requestImgUrl(state.startedApproval.contract);
      state.contractController.resetImages([]);
      state.contractFileController.resetFiles([]);
      state.contractController.addImages(contractData);
      state.contractFileController.addFiles(contractData);
    }
    if((state.startedApproval.propertyCertificate??"").isNotEmpty ){
      state.propertyCertificateController.resetImages([]);
      state.propertyFileController.resetFiles([]);
      List<ImagePickerData> propertyCertificate = await DioClient().requestImgUrl(state.startedApproval.propertyCertificate);
      state.propertyCertificateController.addImages(propertyCertificate);
      state.propertyFileController.addFiles(propertyCertificate);
    }
    if((state.startedApproval.copyIdCard??"").isNotEmpty){
      state.copyIdCardController.resetImages([]);
      state.copyIdFileController.resetFiles([]);
      List<ImagePickerData> copyIdCard = await DioClient().requestImgUrl(state.startedApproval.copyIdCard);
      state.copyIdCardController.addImages(copyIdCard);
      state.copyIdFileController.addFiles(copyIdCard);
    }
    if((state.startedApproval.report??"").isNotEmpty ){
      state.reportController.resetImages([]);
      state.reportFileController.resetFiles([]);
      List<ImagePickerData> report = await DioClient().requestImgUrl(state.startedApproval.report);
      state.reportController.addImages(report);
      state.reportFileController.addFiles(report);
    }
    if((state.startedApproval.construction??"").isNotEmpty ){
      state.constructionController.resetImages([]);
      state.constructionFileController.resetFiles([]);
      List<ImagePickerData> construction = await DioClient().requestImgUrl(state.startedApproval.construction);
      state.constructionController.addImages(construction);
      state.constructionFileController.addFiles(construction);
    }
    if((state.startedApproval.loadReport??"").isNotEmpty){
      state.loadReportController.resetImages([]);
      state.loadReportFileController.resetFiles([]);
      List<ImagePickerData> loadReport = await DioClient().requestImgUrl(state.startedApproval.loadReport);
      state.loadReportController.addImages(loadReport);
      state.loadReportFileController.addFiles(loadReport);
    }
    if((state.startedApproval.other??"").isNotEmpty){
      state.otherController.resetImages([]);
      List<ImagePickerData> other = await DioClient().requestImgUrl(state.startedApproval.other);
      state.otherController.addImages(other);
    }
    update();
  }

  void save() {
    submit(0);
  }

  Future<void> submit(int flag) async {
    state.addStartedApproval.saveOrSubmit = flag;
    state.addStartedApproval.remarks = state.startedApproval.remarks;
    if (state.startedApproval.id != null) {
      state.addStartedApproval.id = state.startedApproval.id;
    }
    state.addStartedApproval.customerInfoId = state.startedApproval.customerInfoId;
    if (state.designPhotoFileController.data.isNotEmpty) {
      if((state.addStartedApproval.designPhoto??[]).isEmpty){
        state.addStartedApproval.designPhoto = [];
      }
      for (var item in state.designPhotoFileController.data) {
        if(!item.isDel) {
          state.addStartedApproval.designPhoto!.add(item.id ?? 0);
        }
      }
    }
    if (state.contractFileController.data.isNotEmpty) {
      if((state.addStartedApproval.contract??[]).isEmpty){
        state.addStartedApproval.contract = [];
      }
      for (var item in state.contractFileController.data) {
        if(!item.isDel) {
          state.addStartedApproval.contract!.add(item.id ?? 0);
        }
      }
    }
    if (state.propertyFileController.data.isNotEmpty) {
      if((state.addStartedApproval.propertyCertificate??[]).isEmpty){
        state.addStartedApproval.propertyCertificate = [];
      }
      for (var item in state.propertyFileController.data) {
        if(!item.isDel) {
          state.addStartedApproval.propertyCertificate!.add(item.id ?? 0);
        }
      }
    }
    if (state.copyIdFileController.data.isNotEmpty) {
      if((state.addStartedApproval.copyIdCard??[]).isEmpty){
        state.addStartedApproval.copyIdCard = [];
      }
      for (var item in state.copyIdFileController.data) {
        if(!item.isDel) {
          state.addStartedApproval.copyIdCard!.add(item.id ?? 0);
        }
      }
    }
    if (state.reportFileController.data.isNotEmpty) {
      if((state.addStartedApproval.report??[]).isEmpty){
        state.addStartedApproval.report = [];
      }
      for (var item in state.reportFileController.data) {
        if(!item.isDel) {
          state.addStartedApproval.report!.add(item.id ?? 0);
        }
      }
    }
    if (state.constructionFileController.data.isNotEmpty) {
      if((state.addStartedApproval.construction??[]).isEmpty){
        state.addStartedApproval.construction = [];
      }
      for (var item in state.constructionFileController.data) {
        if(!item.isDel) {
          state.addStartedApproval.construction!.add(item.id ?? 0);
        }
      }
    }
    if (state.loadReportFileController.data.isNotEmpty) {
      if((state.addStartedApproval.loadReport??[]).isEmpty){
        state.addStartedApproval.loadReport = [];
      }
      for (var item in state.loadReportFileController.data) {
        if(!item.isDel) {
          state.addStartedApproval.loadReport!.add(item.id ?? 0);
        }
      }
    }
    if (state.otherController.data.isNotEmpty) {
      if((state.addStartedApproval.other??[]).isEmpty){
        state.addStartedApproval.other = [];
      }
      for (var item in state.otherController.data) {
        if(!item.isDel) {
          state.addStartedApproval.other!.add(item.id ?? 0);
        }
      }
    }
    if (check(flag)){
      ResponseData response = await DioClient()
          .doPost(ApiConfig.approvalUrl, data: state.addStartedApproval);
      if (response.isok) {
        if (response.code == "0") {
          LoadingUtils.showToast(response.msg ?? "上报成功");
          Get.put(UnApprovalLogic()).callRefresh();
          HDNavigator.close(result: "refresh");
        } else {
          LoadingUtils.showToast(response.msg ?? "上报失败");
        }
      } else {
        LoadingUtils.showToast(response.msg ?? "上报失败");
      }
    }
  }

  Future<void> setData(StartedApproval? tempData) async {
    if (tempData != null) {
      state.startedApproval = tempData;
      await initImgUrl();
      Get.find<RefreshTabLogic>(tag: "3").refreshTabBar();
    }
  }

  bool check(int flag) {
    if(flag == 0){
      return true;
    }
    // if ((state.addStartedApproval.contract??[]).isEmpty) {
    //   LoadingUtils.showToast("请上传合同");
    //   return false;
    // }
    // if ((state.addStartedApproval.propertyCertificate??[]).isEmpty) {
    //   LoadingUtils.showToast("请上传房产证明");
    //   return false;
    // }
    // if ((state.addStartedApproval.copyIdCard??[]).isEmpty) {
    //   LoadingUtils.showToast("请上传身份证复印件");
    //   return false;
    // }
    if ((state.addStartedApproval.report??[]).isEmpty) {
      LoadingUtils.showToast("请上传开工报审表");
      return false;
    }
    // if ((state.addStartedApproval.construction??[]).isEmpty) {
    //   LoadingUtils.showToast("请上传施工方案");
    //   return false;
    // }
    // if ((state.addStartedApproval.designPhoto??[]).isEmpty) {
    //   LoadingUtils.showToast("请上传设计图");
    //   return false;
    // }
    // if ((state.addStartedApproval.loadReport??[]).isEmpty) {
    //   LoadingUtils.showToast("请上传荷载报告");
    //   return false;
    // }
    return true;
  }
}
